﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Xeno.Core;
using ComponentFactory.Krypton.Toolkit;
namespace XenoStudio
{
    public partial class ShadeyEditorArea : UserControl
    {
        public bool Dirty
        {
            get { return _Dirty; }
            set
            {
                if (!value)
                    return;

                _Dirty = true;
                NodeArea.Invalidate();
            }
        }
        private bool _Dirty;
        private BufferedGraphicsContext _BufferContext;
        private BufferedGraphics _Buffer;
        public ShadeyNodeControl ActiveNode { get; set; }
        public XenoEffectParameter ActivePar { get; set; }
        public List<XenoEffectNode> Nodes = new List<XenoEffectNode>();
        private Image _bg = null;
        public Image GetImage(string name)
        {
            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file =
                thisExe.GetManifestResourceStream("XenoStudio.Resource." + name);
            if (file == null)
            {
                return null;
            }
            return Image.FromStream(file);
        }
        protected override void OnPaintBackground(PaintEventArgs pevent)
        { /* Do Nothing */ }

        protected override void OnSizeChanged(EventArgs e)
        {
         
        }
        public ShadeyEditorArea()
        {
            InitializeComponent();
            _bg = GetImage("ShadeyBGSkin.png");
            ActiveNode = null;
            ActivePar = null;
          
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            NodeArea = ShadeyNodeArea;
            NodeArea.NodeLUT = NodeLUT;
        }
        public ShadeyNodeAreaControl NodeArea = null;
        private void SizeGraphicsBuffer()
        {
            if (_Buffer != null)
            {
                _Buffer.Dispose();
                _Buffer = null;
            }

            if (_BufferContext == null)
                return;

            if (NodeArea.DisplayRectangle.Width <= 0)
                return;

            if (NodeArea.DisplayRectangle.Height <= 0)
                return;

            using (Graphics graphics = CreateGraphics())
                _Buffer = _BufferContext.Allocate(graphics, NodeArea.DisplayRectangle);

            Dirty = true;
        }
        public Dictionary<KryptonButton, XenoEffectParameter> NodeLUT = new Dictionary<KryptonButton, XenoEffectParameter>();
        public void RebuildGUI()
        {
            NodeArea.Controls.Clear();
            foreach (var n in Nodes)
            {
                var gn = new ShadeyNodeControl();
                gn.Left = (int)n.UIPos.X;
                gn.Top = (int)n.UIPos.Y;
                gn.Width = (int)n.UIWidth;
                gn.Height =(int) n.UIHeight;
                gn.Node = n;
             
                NodeArea.Controls.Add(gn);
                int dy = (int)n.UIHeight - 16;
                int dx = 2;
                foreach (var i in n.Out)
                {
                    var b = new KryptonButton();
                    b.PaletteMode = PaletteMode.SparkleBlue;
                    gn.Controls.Add(b);
                    b.Left = dx;
                    b.Top = dy;
                    b.Width = 14;
                    b.Height = 14;
                    b.Text = "x";
                    b.Click += new EventHandler(b_Click);
                    NodeLUT.Add(b, i);
                    var l = new KryptonLabel();
                    l.Left = 20;
                    l.Top = dy;

                    l.Width = 120;
                    l.Height = 15;
                    l.Text = i.ID;
                    gn.Controls.Add(l);
                    dy -= 20;
                }
                 dy = (int)n.UIHeight - 16;
                 dx = (int)n.UIWidth - 16;
                foreach (var i in n.In)
                {
                    var b = new KryptonButton();
                    b.PaletteMode = PaletteMode.SparkleOrange;
                    gn.Controls.Add(b);
                    b.Left = dx;
                    b.Top = dy;
                    b.Width = 14;
                    b.Height = 14;
                    b.Text = "x";
                    b.Click +=new EventHandler(b_Click);
                    NodeLUT.Add(b, i);
                    var l = new KryptonLabel();
                    l.Left = (int)n.UIWidth - 100;
                    l.Top = dy;
                    
                    l.Width = 120;
                    l.Height = 15;
                    l.Text = i.ID;
                    gn.Controls.Add(l);
                    dy -= 20;
                }
            }
        }
        
        void b_Click(object sender, EventArgs e)
        {
            var ba = sender as KryptonButton;
            var ep = NodeLUT[ba];
            if (ActivePar != null)
            {
                if (ep != ActivePar)
                {
                    if (ep.To != null)
                    {
                        ep.To.To = null;
                    }
                    if (ActivePar.To != null)
                    {
                        ActivePar.To.To = null;
                    }
                    ep.To = null;
                    ActivePar.To = null;
                    ep.To = ActivePar;
                    ActivePar.To = ep;
                }
                ActivePar = null;

            }
            else
            {
                ActivePar = ep;
                ActiveNode = ba.Parent as ShadeyNodeControl;
                if (ActivePar == null)
                {
                    Environment.Exit(-1);
                }
            }
            NodeArea.ActivePar = ActivePar;
            NodeArea.ActiveNode = ActiveNode;
            NodeArea.Invalidate();
        }
        public void AddNode(XenoEffectNode n)
        {
            if (n.UIPos.X == 0.0f && n.UIPos.Y == 0.0f)
            {
                n.UIPos = new OpenTK.Vector2(100, 150);
            }
            Nodes.Add(n);
            RebuildGUI();
        }
        public int NextDraw = -1;

       

      
        private int MouseX=0, MouseY = 0;
        private void NodeArea_MouseMove(object sender, MouseEventArgs e)
        {
            MouseX = e.X;
            MouseY = e.Y;
        }

        private void NodeArea_SizeChanged(object sender, EventArgs e)
        {

;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
    
        }

    }
}
